package com.simtop.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.userpo.User;
import com.simtop.service.KsServer;
import com.simtop.service.SjServer;
import com.simtop.util.jwtutil.JwtUtil;
import com.simtop.vo.sj.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.simtop.common.Resources.Http;

/**
 * @ClassName: StuController
 * @Description TODO 学生试题训练
 * @Author liuzhaobo
 * @Date 2022/10/31 0031 9:25
 * @Version 1.0
 **/


@RestController
@CrossOrigin
@RequestMapping("/stu")
public class StuController {
    @Autowired
    private SjServer sjServer;

    @Autowired
    private KsServer ksServer;


    /**
     * @Author: liuzhaobo
     * @Description: 学生试题训练
     * @DateTime: 2022/10/21 0021 16:18
     * @Params:
     * @Return
     */
    @RequestMapping("/createStuSj")
    @ResponseBody
    public ServerResponse<Object> createStuSj(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            JSONObject entries = JSONUtil.parseObj(data);
            //先删除
            sjServer.deleteStu(u.getId());
            JSONArray sjId = entries.getJSONArray("sjId");
            if (sjId.size() > 0) {
                //创建题库

                for (Object sjIds : sjId
                ) {
                    //      sjServer.createStuSj(u.getId(), sjIds);
                    sjServer.createStuSj2(u.getId(), sjIds);
                }

            }
            return ServerResponse.createBySuccessMsg("创建成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("创建失败");
        }
    }


    /**
     * @Author: liuzhaobo
     * @Description: 查看该学生创建的训练题库
     * @DateTime: 2022/10/21 0021 16:18
     * @Params:
     * @Return
     */
    @RequestMapping("/findStuSj")
    @ResponseBody
    public ServerResponse<Object> findStuSj(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            JSONObject entries = JSONUtil.parseObj(data);
            Integer userId = entries.getInt("userId");
            List<sjVoSt> allSj = sjServer.getAllSj2(userId);
            for (sjVoSt as : allSj
            ) {
                Integer id = as.getId();
                List<ResourceFj> courseVos = sjServer.getListSjScource(id);
                for (ResourceFj asa : courseVos
                ) {
                    asa.setFjurl(Http + asa.getFjurl());

                }

                if (courseVos.size() > 0) {
                    as.setResourceFjs(courseVos);
                }


            }


            return ServerResponse.createBySuccess("获取成功", allSj);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("创建失败");
        }
    }


    /**
     * @Author: liuzhaobo
     * @Description: 提交试题
     * @DateTime: 2022/10/21 0021 16:18
     * @Params:
     * @Return
     */
    @RequestMapping("/insertStuSj")
    @ResponseBody
    public ServerResponse<Object> insertStuSj(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            JSONObject entries = JSONUtil.parseObj(data);
            Integer sjId = entries.getInt("sjId");
            String answer = entries.getStr("answer");
            Integer userId = u.getId();
            sjServer.insertStuSj(sjId, userId, answer);


            return ServerResponse.createBySuccessMsg("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("提交失败");
        }
    }


    /**
     * @Author: liuzhaobo
     * @Description: 查看该学生创建的训练题库
     * @DateTime: 2022/10/21 0021 16:18
     * @Params:
     * @Return
     */
    @RequestMapping("/findStuSjXl")
    @ResponseBody
    public ServerResponse<Object> findStuSjXl(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            JSONObject entries = JSONUtil.parseObj(data);
            Integer pageNum = entries.getInt("pageNum");
            Integer pageSize = entries.getInt("pageSize");
            String op = entries.getStr("op");
            String px = entries.getStr("px");
            Integer dodId = entries.getInt("dodId");
            Integer tableId = entries.getInt("tableId");
            Integer sjtypeId = entries.getInt("sjtypeId");
            String search = entries.getStr("search");
            Integer userId = entries.getInt("userId");
            PageHelper.startPage(pageNum, pageSize);
            List<sjVoSt> allSj = sjServer.getAllSj3(op, px, dodId, tableId, sjtypeId, search, userId);
            for (sjVoSt as : allSj
            ) {
                Integer id = as.getId();
                List<ResourceFj> courseVos = sjServer.getListSjScource(id);
                for (ResourceFj asa : courseVos
                ) {
                    asa.setFjurl(Http + asa.getFjurl());
                }

                if (courseVos.size() > 0) {
                    as.setResourceFjs(courseVos);
                }


            }
            PageInfo<sjVoSt> pageInfo = new PageInfo<sjVoSt>(allSj);


            return ServerResponse.createBySuccess("获取成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("创建失败");
        }
    }

    /**
     * @Author: liuzhaobo
     * @Description: 删除试题
     * @DateTime: 2022/10/21 0021 16:18
     * @Params:
     * @Return
     */
    @RequestMapping("/deleteStuSjXl")
    @ResponseBody
    public ServerResponse<Object> deleteStuSjXl(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            JSONObject entries = JSONUtil.parseObj(data);
            Integer sjId = entries.getInt("sjId");
            Integer userId = u.getId();
            sjServer.deleteSjStuXl(sjId, userId);

            return ServerResponse.createBySuccessMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("删除失败");
        }
    }

    /**
     * @Author: liuzhaobo
     * @Description: 删除试题
     * @DateTime: 2022/10/21 0021 16:18
     * @Params:
     * @Return
     */
    @RequestMapping("/stuCountXl")
    @ResponseBody
    public ServerResponse<Object> stuCountXl(@RequestBody String data, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            //查询练习记录的统计
            JSONObject entries = JSONUtil.parseObj(data);
            Integer userId = entries.getInt("userId");
            List<Map<Object, Object>> origin = sjServer.stuCountXlOrigin(userId);
            List<Map<Object, Object>> dod = sjServer.stuCountXlDod(userId);
            List<Map<Object, Object>> sjtype = sjServer.stuCountXlType(userId);
            List<Map<Object, Object>> sjtable = sjServer.stuCountXlTable(userId);
            //    List<Map<Object, Object>> com = sjServer.stuCountXlCom(userId);
            //查询已完成的题目
            int isover = sjServer.findisover(userId);
            int allover = sjServer.findallover();
            int notover = allover - isover;
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("name", "已做完");
            objectObjectHashMap.put("value", isover);
            HashMap<Object, Object> objectObjectHashMap2 = new HashMap<>();
            objectObjectHashMap2.put("name", "未做完");
            objectObjectHashMap2.put("value", notover);
            ArrayList<Map<Object, Object>> objects = new ArrayList<>();
            objects.add(objectObjectHashMap);
            objects.add(objectObjectHashMap2);


            XlVo xlVo = new XlVo();
            xlVo.setOrigin(origin);
            xlVo.setDegree(dod);
            xlVo.setSjtype(sjtype);
            xlVo.setSjtable(sjtable);
            xlVo.setComplete(objects);
            //     xlVo.setComplete(com);


            return ServerResponse.createBySuccess(xlVo);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("删除失败");
        }
    }

    /**
     * @Author: liuzhaobo
     * @Description: (训练模式)查看筛选试题列表
     * @DateTime: 2022/10/12 0012 11:59
     * @Params:
     * @Return
     */
    @RequestMapping("/findStuSjXlNew")
    @ResponseBody
    public ServerResponse<Object> findStuSjXlNew(@RequestBody String data, MultipartFile file, HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            String jwt = token.substring(token.lastIndexOf(" ") + 1);
            User u = JwtUtil.unsign(jwt, User.class);
            if (u == null) {
                return ServerResponse.createByErrorMsg("token无效");
            }
            JSONObject entries = JSONUtil.parseObj(data);
            Integer pageNum = entries.getInt("pageNum");
            Integer pageSize = entries.getInt("pageSize");
            String op = entries.getStr("op");
            String px = entries.getStr("px");
            Integer dodId = entries.getInt("dodId");
            Integer tableId = entries.getInt("tableId");
            Integer sjtypeId = entries.getInt("sjtypeId");
            String search = entries.getStr("search");
            Integer userId = entries.getInt("userId");
            PageHelper.startPage(pageNum, pageSize);
            List<sjVo> allSj = sjServer.getAllSj4(op, px, dodId, tableId, sjtypeId, search, userId);
            List<sjVo> allSj2 = sjServer.getAllSj5(op, px, dodId, tableId, sjtypeId, search, userId);
            ArrayList<Object> objects = new ArrayList<>();
            for (sjVo as2 : allSj2
            ) {
                Integer ids = as2.getId();
                objects.add(ids);
            }

            for (sjVo as : allSj
            ) {
                Integer id = as.getId();


                List<ResourceFj> courseVos = sjServer.getListSjScource(id);
                for (ResourceFj asa : courseVos
                ) {
                    asa.setFjurl(Http + asa.getFjurl());

                }

                if (courseVos.size() > 0) {
                    as.setResourceFjs(courseVos);
                }


            }

            PageInfo<sjVo> pageInfo = new PageInfo<sjVo>(allSj);


            KBS kbs = new KBS();
            kbs.setPageInfo(pageInfo);
            kbs.setIdlist(objects);
            return ServerResponse.createBySuccess("获取成功", kbs);


        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("获取失败");
        }
    }
}
